#include "asm/uart.h"


.align 2
.global _init_uart
_init_uart:
	/* disable interrupt */
	la t0, UART_IER
	sb x0, (t0)

	/* Enable DLAB (set baud rate divisor)*/
	la t0, UART_LCR
	li t1, 0x80
	sb t1, (t0)

	la t0, UART_DLL
	li t1, 1
	sb t1, (t0)

	la t0, UART_DLM
	sb x0, (t0)

	/*8 bits, no parity, one stop bit*/
	la t0, UART_LCR
	li t1, 3
	sb t1, (t0)

	/* enable FIFO，clear FIFO, set 14 bytes for threshold*/
	la t0, UART_FCR
	li t1, 0xc7
	sb t1, (t0)

	ret


.global put_uart
put_uart:
loop:
	la t0, UART_LSR
	lbu t1, (t0)
	andi t1, t1, 0x40
	beqz t1, loop

	la t0, UART_DAT
	sb a0, (t0)
	ret

.global put_string_uart
put_string_uart:
	/* At this time the SP stack space has not been allocated,
	 * save the return address ra to the temporary registers
	 */
	mv s8, ra
	mv a5, a0

loop1:
	lbu a0, (a5)
	beqz a0, 1f
	call put_uart
	addi a5, a5, 1
	j loop1
1:
	// restore return addr
	mv ra, s8
	ret
